﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.Programs
{
    public partial class EditProgram : Page
    {
        private readonly ProgramBO _programBO = new ProgramBO();

        /// <summary>
        /// Binds the data.
        /// </summary>
        public void BindData()
        {
            int programId = 0;
            if (int.TryParse(Request.Params["programId"], out programId))
            {
                Program program = _programBO.GetProgramById(programId);
                if (program != null)
                {
                    HiddenFieldProgramId.Value = program.ProgramId.ToString(CultureInfo.CurrentCulture);
                    TextBoxName.Text = program.ProgramName;
                    TextBoxDescription.Text = program.Description;

                    Contact contact = program.Contact;
                    if (contact != null)
                    {
                        TextBoxManagerName.Text = string.Format(CultureInfo.CurrentCulture, "{0} {1}", contact.FirstName,
                                                                contact.Surname);
                        HiddenFieldManagerId.Value = contact.ContactId.ToString(CultureInfo.CurrentCulture);
                    }

                    // If program is exists, set enable for Save button
                    ButtonSave.Enabled = true;
                }
            }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                ViewState["OldName"] = TextBoxName.Text;
                ViewState["filter"] = new ContactFilter();
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            var oldName = ViewState["OldName"] as string;

            var program = new Program();
            program.ProgramId = int.Parse(HiddenFieldProgramId.Value, CultureInfo.CurrentCulture);
            program.ProgramName = TextBoxName.Text;
            program.Description = TextBoxDescription.Text;
            program.ContactId = !string.IsNullOrEmpty(HiddenFieldManagerId.Value)
                                    ? int.Parse(HiddenFieldManagerId.Value, CultureInfo.CurrentCulture)
                                    : (int?) null;
            program.IsActive = ButtonDeactivate.CommandName.Equals("active");

            if (oldName != program.ProgramName)
            {
                if (_programBO.CheckProgramName(program.ProgramName))
                {
                    CheckLabel.Text = "Duplicate Programme Name";
                    return;
                }
            }


            bool checkInsert = _programBO.EditProgram(program);
            if (checkInsert)
            {
                Response.Redirect("~/Program/ProgramList.aspx");
            }
            else
            {
                Response.Write("<script>alert('Insert Programme to database failed ! ')</script>");
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Program/ProgramList.aspx");
        }

        /// <summary>
        /// Handles the Click event of the LinkButtonLookup control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkButtonLookup_Click(object sender, EventArgs e)
        {
            ContactLookupPopup.Popup();
        }

        /// <summary>
        /// Handles the Selecting event of the ContactObjectDataSource control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs"/> instance containing the event data.</param>
        protected void ContactObjectDataSource_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            e.InputParameters["filter"] = ViewState["filter"] as ContactFilter;
        }

        /// <summary>
        /// Handles the Command event of the ButtonInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void ButtonInactive_Command(object sender, CommandEventArgs e)
        {
            ButtonDeactivate.CommandName = e.CommandName == ("active")
                                               ? "deactive"
                                               : "active";
            ButtonDeactivate.Text = e.CommandName == ("active")
                                        ? "Activate"
                                        : "Deactivate";
            LiteralIsActive.Text = ButtonDeactivate.CommandName.Equals("active") ? "Yes" : "No";
        }
    }
}